﻿using System.Diagnostics;

namespace async_await
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Test1();
        }

        public static async Task Test1()
        {
            string thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("1 当前线程ID：{0}", thread_id));

            // await 运算符应用于一个异步方法中的任务以挂起该方法的执行，直到等待任务完成。 表示正在进行的工作的任务。
            int sum = await DoSomeWork();

            Console.WriteLine(string.Format("{0}", sum));

            thread_id = Environment.CurrentManagedThreadId.ToString();
            Console.WriteLine(string.Format("3 当前线程ID：{0}", thread_id));
        }

        public static Task<int> DoSomeWork()
        {
            Stopwatch sw = Stopwatch.StartNew();
            int thread_id = Thread.CurrentThread.ManagedThreadId;
            Console.WriteLine(string.Format("2 当前线程ID：{0}", thread_id));

            int sum = 0;
            for (int i = 0; i < 100_000; i++)
            {
                sum += i;
            }
            Console.WriteLine($"DoSomeWork执行了:{sw.ElapsedMilliseconds}毫秒");
            return Task.FromResult(sum);
        }
    }
}
